//
//  PictureViewController.m
//  MiaoTu
//
//  Created by dllo on 16/9/15.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "PhotoAlbumViewController.h"
#import "PhotoAlbumCollectionCell.h"
#import "Piclist.h"
#import "UIView+Frame.h"
#import "Masonry.h"

#define kPhotoAlbumCollectionCell @"PhotoAlbumCollectionCellIdentifier"

@interface PhotoAlbumViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray<Piclist *> *picArray;
@property (nonatomic, strong) UILabel *currentLabel;
@end

@implementation PhotoAlbumViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc] init];
//    fl.itemSize = CGSizeMake(self.view.dk_width, self.view.dk_height - 64);
    fl.itemSize = self.view.bounds.size;
    fl.minimumInteritemSpacing = 0;
    fl.minimumLineSpacing = 0;
    fl.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:fl];
    
    [self.view addSubview:_collectionView];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [_collectionView registerClass:[PhotoAlbumCollectionCell class] forCellWithReuseIdentifier:kPhotoAlbumCollectionCell];
    _collectionView.backgroundColor = [UIColor blackColor];
    _collectionView.pagingEnabled = YES;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    
    self.picArray = self.picDic[@"picArray"];
    NSNumber *currentIndex = self.picDic[@"currentImageIndex"];
    self.collectionView.contentOffset = CGPointMake(self.view.dk_width * currentIndex.integerValue, 0);
    
    self.currentLabel = [UILabel new];
    [self.view addSubview:_currentLabel];
    [_currentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.view.mas_top).offset(20);
    }];
    _currentLabel.textColor = [UIColor whiteColor];
    _currentLabel.text = [NSString stringWithFormat:@"%zd/%zd", currentIndex.integerValue + 1, _picArray.count];
    
    UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [downloadButton setImage:[UIImage imageNamed:@"downloadImage"] forState:UIControlStateNormal];
    [self.view addSubview:downloadButton];
    [downloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(20);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(20);
        make.width.height.mas_equalTo(50);
    }];
    [downloadButton addTarget:self action:@selector(downloadImage) forControlEvents:UIControlEventTouchUpInside];
    
    UITapGestureRecognizer *popTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(popAction)];
    [self.view addGestureRecognizer:popTap];
}

- (void)popAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _picArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PhotoAlbumCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kPhotoAlbumCollectionCell forIndexPath:indexPath];
    cell.picList = _picArray[indexPath.item];
    return cell;;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSInteger currentIndex = scrollView.contentOffset.x / self.view.dk_width;
    _currentLabel.text = [NSString stringWithFormat:@"%zd/%zd", currentIndex + 1, _picArray.count];
}

- (void)downloadImage {
    NSLog(@"downLoad");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
